package com.xuyiwei.mayi.service;

import org.I0Itec.zkclient.ZkClient;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by xuyiwei on 2019/4/24.
 */
public class ZkSocketServer {
    public static void main(String[] args) throws IOException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ZkSocketServer.startSocket(8083);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ZkSocketServer.startSocket(8084);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ZkSocketServer.startSocket(8085);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        t3.start();
    }

    private static void registServer(int port) {
        ZkClient zk = new ZkClient("127.0.0.1:2181", 60000, 1000);
        String path = "/zkSocket/zkSocket_"+port;
        if(!zk.exists("/zkSocket")){
            zk.createPersistent("/zkSocket");
        }
        if(zk.exists(path)){
            zk.delete(path);
        }
        zk.createEphemeral(path,"127.0.0.1:"+port);

    }

    public static void startSocket(int port) throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("服务端启动");
        //把这个服务器注册到zk的临时节点上去
        registServer(port);
        while (true){
            Socket socket = serverSocket.accept();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    InputStream in = null;
                    BufferedReader br = null;
                    PrintWriter pw = null;
                    try {
                        StringBuffer sb = new StringBuffer();
                        sb.append(port+"接收到请求");
                        in = socket.getInputStream();
                        br = new BufferedReader(new InputStreamReader(in));
                        pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                        String str = null;
                        while (true) {
                            str = br.readLine();
                            if (str == null)
                                break;
                            System.out.println(port+"Receive : " + str);
                            System.out.println(port+"Hello, " + str);
                        }

                        pw.write("aaaa");
                        pw.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
        }

    }
}
